package com.worldvision.main.util;

import java.util.List;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.seleniumemulation.WaitForCondition;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class UTIL 
{

	public static boolean checkInPage(WebDriver driver, String text) 
	{
		// TODO Auto-generated method stub
		List<WebElement> wb = driver.findElements(By.tagName("body"));
		
		for(WebElement chileWebElement:wb)
		{
			if(chileWebElement.getText().contains(text)){
				return true;
			}
				
		}
		return false;
	}
	public static void fillFormData(WebDriver driver,Map<String, String> data) 
	{	
		String value;
	
		// TODO Auto-generated method stub
		//loop through all the keys in the hash map
				for(String key:data.keySet())
				{
						
					//creating try catch to catch alert box and clicking ok on confirmation
					try
					{
						value = data.get(key);
						if(value.contains("label="))
						{
							value = value.replace("label=","");
							//send the text to the object which have name as in (keys in data sheet)
							if(driver.findElement(By.name(key)).isDisplayed())
							{
								System.out.println(key+value);
								(new Select(driver.findElement(By.name(key)))).selectByVisibleText(value);//driver.findElement(By.name(s)).sendKeys(data.get(s));
							}
								else
								System.out.println(key);
						}
						//check if data is given
						else if((key).contains("radio"))
								{
									if(driver.findElement(By.name(key.replace("radio=", ""))).isDisplayed())
									{
										System.out.println(key+value);
										//send the text to the object which have name as in (keys in data sheet)
										driver.findElement(By.name(key.replace("radio=", ""))).click();
										//driver.findElement(By.name(key)).sendKeys(data.get(key));
									}
									else
									System.out.println(key);
								}
						else if(data.get(key)!="")
						{
							if(driver.findElement(By.name(key)).isDisplayed())
							{
								System.out.println(key+value);
								//send the text to the object which have name as in (keys in data sheet)
								driver.findElement(By.name(key)).clear();
								driver.findElement(By.name(key)).sendKeys(data.get(key));
							}
							else
								System.out.println(key);
						}
				
					
					}
					catch(UnhandledAlertException e)
					{
						//driver.switchTo().alert().accept();
					}
				}
				
	}
	public static void waitForSec(int i)
	{
			try {
				Thread.sleep(i);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public static boolean isElementPresent(WebDriver driver,By by)
	{
		return (driver.findElements(by).size()!=0);
	}
	public static void selectElement(WebDriver driver, By id, String string) {
		
		(new Select(driver.findElement(id))).selectByVisibleText(string);
		
	}
	public static void waitForText(final WebDriver driver,final String string) {
		
		WebDriverWait w = new WebDriverWait(driver,30);
		ExpectedCondition ec = new ExpectedCondition(){
		
//			public void apply()
//			{
//				checkInPage(driver, string);
//			}

			@Override
			public Object apply(Object arg0) {
				// TODO Auto-generated method stub
				return checkInPage(driver, string);
			}

//			@Override
//			public Object apply(Object arg0) {
//				// TODO Auto-generated method stub
//				return null;
//			}
		};
		w.until(ec);
	}
}
